<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>背包记录</title>
    <link rel="stylesheet"  href="src/css/base.css?__version=9.4">
    <link rel="stylesheet"  href="src/css/pages.css?__version=9.4">
    <script type="text/javascript" src="src/js/jquery-1.10.1.min.js?__version=9.4"></script>
    <script type="text/javascript" src="src/js/rem.js?__version=9.4"></script>
    <script type="text/javascript" src="src/js/dropload.js?__version=9.4"></script>
    <script type="text/javascript" src="src/js/init.js?__version=9.4"></script>

	<script src="https://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
	<script src="/src/js/share.js?__version=9.4"></script>
	<script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?8ed2efe5b24e14f351f4d88ba2eb3462";
      var s = document.getElementsByTagName("script")[0]; 
      s.parentNode.insertBefore(hm, s);
    })();
    </script>
</head>
<body>
	<div class="init_content" style="display: none;">
    <div class="warp backpack-warp">
    	
        <section class="caption" id="htmlDataList"></section>
    </div>
	</div>
    <div class="init_loading">
        <img src="src/img/page_loading.gif">
    </div>
</body>
<script>
addheader("背包记录","/personal.html")
$(function(){
	checkUserLogin();

	fetchData("/OpenAPI/V1/Backpack/getAll",{page:1,token:token},function(data){

		var htmlDataList="";
		var nullNum = 0;
		if(data.list.length>0){
			for(var i=0;i<data.list.length;i++){
				if(data.list[i]['toy_type']&&data.list[i]['toy_type']=='1'||data.list[i]['toy_type']=='0'){
					htmlDataList += '<label class="item flex">';
					htmlDataList += '<img src='+data.list[i].cover+' width="50px;" heoght="50px;">';
					htmlDataList += '<div class="info">';
					htmlDataList += '<p class="name text-beyond">'+data.list[i].title+'<span>兑换'+data.list[i].to_user_price+'金币</span></p>';
					htmlDataList += '<p class="time text-beyond">'+data.list[i].create_at+'</p>';
					htmlDataList += '</div>';
					htmlDataList += '<span class="align-right ">';
					htmlDataList += '<input class="icon-check" type="checkbox" name="ids[]" value="'+data.list[i].backpack_id+'">';
					htmlDataList += '<input class="icon-check" type="hidden" id="toy_'+data.list[i].backpack_id+'" value="'+data.list[i].toy_id+'">';
					htmlDataList += '<input class="icon-check" type="hidden" id="money_'+data.list[i].backpack_id+'" value="'+data.list[i].to_user_price+'">';
					htmlDataList += '<input class="icon-check" type="hidden" id="name_'+data.list[i].backpack_id+'" value="'+data.list[i].title+'">';
					htmlDataList += '<input class="icon-check" type="hidden" id="img_'+data.list[i].backpack_id+'" value="'+data.list[i].cover+'">';
					htmlDataList += '</span>';
					htmlDataList += '</label>';
				}else{
					nullNum++;
				}
			}
			if(nullNum == data.list.length){
				htmlDataList="<div class='nulldefault'><img src='src/img/nulldefault.png'><p>暂无数据</p></div>";
			}
		}else{
			htmlDataList="<div class='nulldefault'><img src='src/img/nulldefault.png'><p>暂无数据</p></div>";
		}
		$("#htmlDataList").html(htmlDataList);
		if($("#htmlDataList .item").length>0){
			$("#htmlDataList").after('<div style="text-align:center;padding:5px 0;font-size:14px;color:#ff7979;">一个只能兑换金币，两个才能邮寄</div><button type="button" class="btn btn-green" style="margin-top:0;" onclick="operation(\'exchange\')">兑换金币</button><button type="button" class="btn btn-yellow" onclick="operation(\'order\')"  style="color:#545454;">提交订单</button>')
		}
	})
});

function operation(type){
	var back_ids = new Array();
	var toy_ids = new Array();
	var toys = new Array();
	$("input[name='ids[]']:checked").each(function(i){//把所有被选中的复选框的值存入数组
		var back_id = $(this).val();//背包ID
		var toy_id = $('#toy_'+back_id).val();//娃娃ID
		var money = $('#money_'+back_id).val();//兑换金额
		var name = $('#name_'+back_id).val();//娃娃昵称
		var img = $('#img_'+back_id).val();//娃娃图片
		if(toy_ids.indexOf(toy_id)>=0){
			for(var i=0;i<toys.length;i++){
				if(toys[i]["toy_id"]==toy_id){
					toys[i]["money"]=Number(toys[i]["money"])+Number(money);
					toys[i]["num"]=Number(toys[i]["num"])+1;
				}
			}
		}else{
			toys.push({'toy_id':toy_id,'money':money,'name':name,'img':img,'num':1});
			toy_ids.push(toy_id);
		}
		
		back_ids.push(back_id);
    });
	
	if(back_ids.length == 0){
		alert('请选择');
		return;
	}
	if(type=="order"){
		if(back_ids.length<2){
			alert("请选择至少两个娃娃哟")
			return;
		}
	}
	console.log(back_ids)
	console.log(toys)
	//存cookie
	//document.cookie = 'back_ids='+JSON.stringify(back_ids);
	//document.cookie = 'toys='+JSON.stringify(toys);
	 cookieSet('back_ids',JSON.stringify(back_ids),300);
	 cookieSet('toys',JSON.stringify(toys),'300');
	// console.log(cookieGet('back_ids'));
 //    console.log(cookieGet('toys'));
	switch(type){
		//兑换金币数据
		case 'exchange':
			window.location.href='/changeCoins.html';
			break;
		//提交提单数据
		case 'order':
			window.location.href='/orderDetails.html';
			break;
		default:
			alert('操作错误');
			break;
	}
}
</script>
</html>
